//
//  VehicleMaintenanceDetailViewController.m
//  阿帕
//
//  Created 阿帕 on 2020/1/5.
//  Copyright © 2020 GCY. All rights reserved.
//

//#define header_h 365
//620 365
#import "VehicleMaintenanceDetailViewController.h"
#import "VehicleMaintenanceStepsCell.h"
#import "VehicleMaintenanceDetailHeader.h"

@interface VehicleMaintenanceDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    CGFloat _headerHeight;
}
@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)NSDictionary *detailDic;

@property (strong , nonatomic)VehicleMaintenanceDetailHeader *headerView;

@end

@implementation VehicleMaintenanceDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _headerHeight = 0;
    
    [self setupBaseView];
    
    [self vehicleMaintenanceDetailRequest];
    
    [self vehicleMaintenanceReviewRecordRequest];
}

- (void)setupBaseView {
    
    self.navigationItem.title = @"车辆维修详情";

    self.view.backgroundColor = BackgroundColor;
    
}


- (void)vehicleMaintenanceDetailRequest {
    WEAKSELF
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], VEHICLEREPAIRAPPLY, _code) withParameter:@{ @"token":[UserDefaultUtil valueForKey:USER_TOKEN],} withVC:self netName:@"车辆维修详情" success:^(id response, NSString *msg) {
        
        float valueMaxWidth = ScreenW-70-40;
        
        CGSize contentSize = [DCSpeedy dc_calculateTextSizeWithText:[NSString nullToString:response[@"title"]] WithTextFont:14 WithMaxW:valueMaxWidth];
        CGSize detailSize = [DCSpeedy dc_calculateTextSizeWithText:[NSString nullToString:response[@"details"]] WithTextFont:14 WithMaxW:valueMaxWidth];
        
        if ([response[@"isReportComplete"] integerValue] == 1) {
            _headerHeight = 620;
        } else {
            _headerHeight = 300 + contentSize.height + detailSize.height;
            
//            40 + 40 + 165
        }
        
        _detailDic = response;
        
        [weakSelf.view addSubview:weakSelf.tableView];
        
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

- (void)vehicleMaintenanceReviewRecordRequest {
    WEAKSELF
    NSDictionary *parmas = @{
        @"applyFromCode": _code,//审批来源编码
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], REVIEWRECORD_LIST) withParameter:parmas withVC:self netName:@"审批流程记录" success:^(id response, NSString *msg) {
        
        if ([DCSpeedy isBlankArr: response] == NO) {
            [weakSelf.dataArray addObjectsFromArray:[MaintainStepModel mj_objectArrayWithKeyValuesArray:response]];
            
            NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
            
            [weakSelf.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
        }
        
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VehicleMaintenanceStepsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = self.dataArray[indexPath.row];
    
    MaintainStepModel *model = self.dataArray[indexPath.row];
    //是否是审批的最后决策节点 1是
    if (model.applyStatus == 1) {
        cell.vLineView.backgroundColor = Theme_ColorValue;
        cell.stepNumLabel.backgroundColor = Theme_ColorValue;
    } else {
        cell.vLineView.backgroundColor = Light_TextColor;
        cell.stepNumLabel.backgroundColor = Light_TextColor;
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return _headerHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    _headerView = [[VehicleMaintenanceDetailHeader alloc] initWithFrame:CGRectMake(0, 0, ScreenW, _headerHeight)];
    _headerView.detailDic = _detailDic;
    return _headerView;
}
#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];

        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([VehicleMaintenanceStepsCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
    }
    
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
@end
